﻿<?php get_header(); ?>
<div id="all_content">
	<?php get_sidebar(); ?>
	<div id="content_right">
		<div class="wrapper">
			<div class="all_content_news">
			<?php
				/*
				 * get the first sub-category - if user click on the menu (main-category), the $cat is not the sub-category.
				 */
				$args = array( 'parent' => $cat, 'hide_empty' => false );
				$categories = get_categories( $args );
				$categoryID = $categories ? $categories[0]->cat_ID : $cat;
				
				/* 
				 * Get the posts which are belong to a specific sub-category
				 */
				$args=array(
					'cat' => $categoryID,
					'post_type' => 'post',
					'post_status' => 'publish',
					'posts_per_page' => -1
				);
				$my_query = new WP_Query($args);
				
				if( $my_query->have_posts() ) {
					while ($my_query->have_posts()) : $my_query->the_post(); ?>
						<?php
							$post_thumbnail_id = get_post_thumbnail_id( get_the_ID() );
							$link_thumnail = wp_get_attachment_url ( $post_thumbnail_id );
							if ( empty ( $post_thumbnail_id ) ) {
								$link_thumnail =  get_bloginfo('template_url')."/images/default_avatar.jpg";
							}
							/*
							 * For the category which has more than one posts
							 */
							// if ($my_query->post_count > 1 ): 
							if ( get_category($categoryID)->description != 'one_post' ): ?>
								<div class="all_news">
									<div class="avatar_all_news">
										<a href="#"><img src="<?php echo $link_thumnail; ?>" width="132px" height="106px"></a>
									</div>
									<div class="information_general">
										<div class="title_all_news"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div>
										<div class="short_news"><?php the_excerpt(); ?></a></div>
									</div>
								</div>
								<div class="border_gradient"></div>
						<?php 
							else: /* For the category which has only one post*/
						?>
							<div id="title_detail_news"> <?php the_title()?> </div>
							<div class="avatar_detail_news"> 
								<a href="#"><img src="<?php echo link_thumnail; ?>" width="205px" height="168px"></a> 
							</div>
						<?php
							the_content();
						endif; ?>
					  <?php
					endwhile;
				}

				wp_reset_postdata()
			?>
			</div>
		</div>
		<!-- mousewheel plugin -->
		<script src="<?php bloginfo('template_url'); ?>/js/jquery-ui-1.9.1.custom.min.js"></script>
		<script src="<?php bloginfo('template_url'); ?>/js/jquery.mCustomScrollbar.js"></script>
		<script src="<?php bloginfo('template_url'); ?>/js/jquery.mousewheel.min.js"></script>
		
		<script>
			(function ($) {
				$(window).load(function () {
					$(".all_content_news").mCustomScrollbar({ set_height: 538 });
				});
			})(jQuery);
		</script>
		
	</div>
	<div style="clear: both"></div>
</div>
<?php get_footer(); ?>